/*
Copyright (C) Vincenzo Tilotta 2012 <tailot@gmail.com>

sprite.js is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your option) any later version.

sprite.js is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
function Sprite(typesprite, initPosition,pathimage,idcanvas){
		this.idcanvas = '';
		this.id = '';
		this.type = '';
		
		/*
		 * INIT
		 */
		this.init = function(){
			this.type = typesprite;
			this.idcanvas = idcanvas;
			
			var t = new Date();
			this.id = "s"+t.getTime();
			$('#'+this.idcanvas).append('<img id="'+this.id+'" class="'+this.type+'" src="'+pathimage+'" alt=" " />');
			$('#'+this.id).css("left",initPosition[0]);
			$('#'+this.id).css("top",initPosition[0]);
		};
		
		/*
		 * Lo sprite si muove cliccando su un punto del canvas
		 */
		this.moveclick = function(){
				$("#"+this.idcanvas).bind('click',{id: this.id},function(e){
					pastX = parseInt($('#'+e.data.id).css("left").replace("px",""));
					pastY = parseInt($('#'+e.data.id).css("top").replace("px",""));
				
					var angle = parseInt(angleByCoordinates(pastX,pastY,e.pageX,e.pageY));
					rotate(e.data.id,angle);
					moveXY(e.pageX,e.pageY,e.data.id);
				});
		};
		
		/*
		 * Abilita o Disabilita lo sprite a muoversi con il click del mouse
		 */
		this.enablemove = function(){
				this.moveclick();
		};
		
		/*
		 * Muove lo sprite in posizione posX posY creando l'animazione
		 */
		this.move = function(posX, posY){
			moveXY(posX,posY,this.id);
		};
		
		/*
		 * Muove lo sprite in posizione posX posY senza animazione
		 */
		this.movenoanimation = function(posX, posY){
			moveXYnoanimate(posX,posY,this.id);
		};
		/*
		 * Abilita dall'esterno la rotazione in loop
		 */
		this.enablerotate = function(deg, myspeed){
			looprotate(deg, myspeed, this.id);
		};
		/*
		 * Rotazione in loop
		 */
		looprotate = function(deg, myspeed,myid){
				var degupgrade = deg + 10;
				if(degupgrade == 360){
					degupgrade = 0;
				}
				rotate(myid,degupgrade);
				setTimeout("looprotate("+degupgrade+","+myspeed+",'"+myid+"')",myspeed);
		};
		
		/*
		 * Abilita la caduta dello sprite
		 */
		this.enablefall = function(){
			var currenttop = parseInt($('#'+this.id).css("top").replace("px",""));
			var spacefall = parseInt($('#'+idcanvas).css('height').replace("px","")) - currenttop;
			$('#'+this.id).animate({top:'+='+spacefall},5000,function(){
				$('#'+this.id).remove();
			});
		};
		
		/*
		 * Abilita la lievitazione dello sprite
		 */
		this.enablerises = function(){
			var currenttop = parseInt($('#'+this.id).css("top").replace("px",""));
			$('#'+this.id).animate({top:'-='+currenttop},5000,function(){
				$('#'+this.id).remove();
			});
		};
		this.init();		
};